package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.DuolaCarService;
import com.qike.duola.service.DuolaFormulaService;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "配比控制器")
@CrossOrigin
public class FormulaController {

    private static Logger logger = LoggerFactory.getLogger(FormulaController.class);

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaFormulaService duolaFormulaService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加配比
     *
     * @return
     */
    @PostMapping("/formulaAdd")
    @ApiOperation("添加配比")
    @ResponseBody
    public BaseResult formulaAdd(@RequestBody FormulaAddDto formulaAddDto) {
        try {
            duolaFormulaService.formulaAdd(formulaAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 编辑配比
     *
     * @return
     */
    @PostMapping("/formulaEdit")
    @ApiOperation("编辑配比")
    @ResponseBody
    public BaseResult formulaEdit(@RequestBody FormulaEditDto formulaEditDto) {
        try {
            duolaFormulaService.formulaEdit(formulaEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除配比
     *
     * @return
     */
    @PostMapping("/formulaDel")
    @ApiOperation("删除配比")
    @ResponseBody
    public BaseResult formulaDel(@RequestBody DeptDelDto deptDelDto) {
        try {
            duolaFormulaService.formulaDel(deptDelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 配比列表
     *
     * @return
     */
    @GetMapping("/formulaList")
    @ApiOperation("配比列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = FormulaListVo.class)
    })
    public BaseResult formulaList( FormulaListDto formulaListDto) {
        try {
            FormulaListVo vo = duolaFormulaService.formulaList(formulaListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 配比下拉列表
     *
     * @return
     */
    @GetMapping("/formulaPullList")
    @ApiOperation("配比下拉列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = FormulaListVo.class)
    })
    public BaseResult formulaPullList( String name,Integer goodsId) {
        try {
            List<FormulaPullListVo> vo = duolaFormulaService.formulaPullList(name,goodsId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 配比详情
     *
     * @return
     */
    @ApiOperation("配比详情")
    @GetMapping("/formulaInfo")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = FormulaInfoVo.class)
    })
    public BaseResult formulaInfo(Integer id) {
        try {
            FormulaInfoVo vo = duolaFormulaService.formulaInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 根据砼品种获取模板数据
     *
     * @return
     */
    @ApiOperation("根据砼品种获取模板数据")
    @GetMapping("/getTemplateByConcreteId")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = FormulaInfoVo.class)
    })
    public BaseResult getTemplateByConcreteId(Integer concreteId) {
        try {
            List<FormulaGoodsListVo> vo = duolaFormulaService.getTemplateByConcreteId(concreteId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 配比编辑物料跳转
     *
     * @return
     */
    @ApiOperation("配比编辑物料跳转")
    @GetMapping("/formulaEditGoodsJump")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = FormulaInfoVo.class)
    })
    public BaseResult formulaEditGoodsJump(Integer id) {
        try {
            FormulaInfoVo vo = duolaFormulaService.formulaEditGoodsJump(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 配比添加跳转
     *
     * @return
     */
    @ApiOperation("配比添加跳转")
    @GetMapping("/formulaAddGoodsJump")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = FormulaAddGoodsJumpVo.class)
    })
    public BaseResult formulaAddGoodsJump() {
        try {
            List<FormulaAddGoodsJumpVo> vos = duolaFormulaService.formulaAddGoodsJump();
            return RetResponse.makeOKRsp(vos);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
